proc ban {nick handle chan otbn1 otbn botnet2 botnet3 host} {
  global home botnick bcast


# ERROR TRAPS before processing command
# -------------------------------------

  # Verify sufficient parameters
  if {$otbn1 == ""} {
    putquick "NOTICE $nick :\0034ERROR - Invalid Syntax - Should Be - (/msg) $botnick ban <#channel> <nick> OR <mask> <time> <reason>"
    return 0

  # Verify bot is opped
  } elseif {![botisop $chan]} {
    putquick "NOTICE $nick :\0034ERROR - Bot dosen't have op status (+o) on $chan"
    return 0

  # Verify not banning the bot
  } elseif {[string tolower $otbn1] == [string tolower $botnick]} {
    putquick "NOTICE $nick :\0034ERROR - Unable To Ban This Bot.  Use\00312 $botnick part \0034instead\003"
    return 0
  }

# Set Ban Length
# ============================================================
  set time -1
  set reason $botnet2

putlog "OTBN-$otbn  TIME-$time"

  if {[string match "*m" $otbn] == 1} {
    putlog "m"
    set time [string range $otbn 0 [expr [string length $otbn] - 2]]
    set reason $botnet3
  } elseif {[string match "*h" $otbn] == 1} {
    putlog "h"
    set time [expr [string range $otbn 0 [expr [string length $otbn] - 2]] * 60]
    set reason $botnet3
  } elseif {[string match "*d" $otbn] == 1} {
    putlog "d"
    set time [expr [string range $otbn 0 [expr [string length $otbn] - 2]] * 60 * 24]
    set reason $botnet3
  } elseif {[string match "*w" $otbn] == 1} {
    putlog "w"
    set time [expr [string range $otbn 0 [expr [string length $otbn] - 2]] * 60 * 24 * 7]
    set reason $botnet3
  }

putlog "Post interpretative= OTBN-$otbn  TIME-$time"

# Error traps for Ban Length
# ============================================================

  # Added in X-Bot 2003 SP-1, from OTBN.tcl 2.2
  if {[string tolower $otbn] == "perm"} {
    set time 0
    set otbn "Permanent"
    set reason $botnet3
  }

  # Make sure ban is not too long
  if {$time > 72000} {
    set time 72000
    putquick "NOTICE $nick :\00312You specified a time greater than 50 days.  This bot cannot handle bans longer than 50 days that are not permanent.  Defaulting to a 50 day ban."
  }

  # Default value was -1, use default time if this is still the case
  if {$time == -1} {
    set time 60
    set reason $botnet3

    putquick "NOTICE $nick :\00312You Did Not Specify a Ban Time. Defaulted to 1 Hour." 
  }

  # Make sure no dipshit put in a negative value
  if {$time < 0} {
    set time 60
    set reason $botnet3

    putquick "NOTICE $nick :\00312You Did Not Specify a Valid Ban Time. Defaulted to 1 Hour." 
  }

  set otbn [duration [expr $time * 60]]
  
# Issue Proper Ban
# ============================================================

  # If banned user is not on the channel?
  if {![onchan $otbn1 $chan]} {
    set hand2 [finduser $otbn1]

    # if the user has sufficient privileges to ban this user
    if {[checkaccess $handle $chan $hand2]} {
      
      # No ban reason defined?  If not, define one.
      if {$reason == ""} {
        set banreason "$nick banned you"
      } else {
        set banreason "$nick banned you for $reason"
      }

      putlog "!$nick ($host)! ban $chan $otbn1 $otbn"

      newchanban $chan $otbn1 $nick $banreason $time

      # Process ban from banlist
      # ------------------------
      set counter 1
      foreach newban [banlist $chan] {
        set mask [lindex $newban 0]
        set reason [lindex $newban 1]
        set expire [lindex $newban 2]
        set added [lindex $newban 3]
        set setter [lindex $newban 5]
        set dateexpired [ctime $expire]
        set dateadded [ctime $added]

        if {$setter == $nick && $reason == $banreason && [expr $added - [unixtime]] < 10 } {
          # If perm ban, note it never expires
          if {$time == 0} {
            set xexpiration "Never"
          } else {
            set xexpiration [ctime $expire]
          }
          set bannum $counter 
        }
 
        incr counter
      }

      putquick "NOTICE $nick :\0033Added new channel ban in '\0034$chan\003' - Ban Number: \002\0034$bannum - All Ban Numbers have been updated."
      putquick "NOTICE $chan :All Ban Numbers have been updated.  Newest Ban Number: \002$bannum\002."


      if {$bcast == "1"} {
        putquick "PRIVMSG $home :\002$nick\002 ($host) requested me to ban \002$otbn1\002 in $chan. Reason: $reason. Length Of Ban: $otbn.  Expires: $xexpiration."
      }

      return 0
    }

    putquick "NOTICE $nick :\0034ERROR - Unable To Execute Command - Not Enough Permissions - Permission Denied."
    return 0
  }

  set hand2 [nick2hand $otbn1 $chan]

  if {$hand2 == $handle} {
    putquick "NOTICE $nick :\0034ERROR - Sorry, you may not ban yourself."
    return 0
  }

  if {[checkaccess $handle $chan $hand2]} {
    set hostname [getchanhost $otbn1 $chan]

# Incompatible to Eggdrop 1.6
#    if {[string match "~*" $hostname]} {
#      set hostname [string range $hostname 1 end]
#    }

    set banhostname [maskhost *$hostname]

    if {$reason == ""} {
      putlog "!$nick ($host)! ban $chan $otbn1 ($banhostname) $otbn"

      set banreason "$nick banned you"

      newchanban $chan $banhostname $nick $banreason $time

      # Process ban from banlist
      # ------------------------
      set counter 1
      foreach newban [banlist $chan] {
        set mask [lindex $newban 0]
        set reason [lindex $newban 1]
        set expire [lindex $newban 2]
        set added [lindex $newban 3]
        set setter [lindex $newban 5]
        set dateexpired [ctime $expire]
        set dateadded [ctime $added]

        if {$setter == $nick && $reason == $banreason && [expr $added - [unixtime]] < 10 } {
          set xexpiration [ctime $expire]
          set bannum $counter 
        }
 
        incr counter
      }

      putquick "NOTICE $nick :\0033Added new channel ban \0034\002$otbn1 ($banhostname)\002\0033in channel record '\0034$chan\003' - Ban Number: \002\0034$bannum"
      putquick "NOTICE $chan :All Ban Numbers have been updated.  Newest Ban Number: \002$bannum\002."
  
      if {$bcast == "1"} {
        putquick "PRIVMSG $home :\002$nick\002 ($host) requested me to ban \002$otbn1\002 ($banhostname) in $chan. Reason: N/A. Length Of Ban: $otbn.  Expires: $xexpiration."
      }
  
      putquick "KICK $chan $otbn1 :Reason:\00312 $banreason \003- Length:\00312 $otbn \003- Expires: \00312$xexpiration \003- Ban Number \00312$bannum\003 ."
      return 0
    }

    putlog "!$nick ($host)! ban $chan $otbn1 ($banhostname) $otbn $reason"

    set banreason "$nick banned you for $reason"
    newchanban $chan $banhostname $nick $banreason $time 

      # Process ban from banlist
      # ------------------------
      set counter 1
      foreach newban [banlist $chan] {
        set mask [lindex $newban 0]
        set reason [lindex $newban 1]
        set expire [lindex $newban 2]
        set added [lindex $newban 3]
        set setter [lindex $newban 5]
        set dateexpired [ctime $expire]
        set dateadded [ctime $added]

        if {$setter == $nick && $reason == $banreason && [expr $added - [unixtime]] < 10 } {
          set xexpiration [ctime $expire]
          set bannum $counter 
        }
 
        incr counter
    }

    putquick "NOTICE $nick :\0033Added new channel ban \0034\002$otbn1 ($banhostname)\002\0033in channel record '\0034$chan\003' - Ban Number: \002\0034$bannum"
    putquick "NOTICE $chan :All Ban Numbers have been updated.  Newest Ban Number: \002$bannum\002."

    if {$bcast == "1"} {
      putquick "PRIVMSG $home :\002$nick\002 ($host) requested me to ban \002$otbn1\002 ($banhostname) in $chan. Reason: $reason.  Length: $otbn1.  Expires: $xexpiration."
    }

    putquick "KICK $chan $otbn1 :Reason:\00312 $banreason \003- Length:\00312 $otbn \003- Expires: \00312$xexpiration \003- Ban Number \00312$bannum\003 ."
    return 0
  }
  putquick "NOTICE $nick :\0034ERROR - Unable To Execute Command - Not Enough Permissions - Permission Denied."
}










